package com.zpb.framework.handler;

//import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson.JSON;
import com.zpb.framework.domain.ApiResult;
import com.zpb.framework.domain.enums.ResultCodeEnum;
import com.zpb.framework.utils.DictCodeUtil;
import com.zpb.framework.utils.ServletUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/11
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        accessDeniedException.printStackTrace();
        ApiResult<Object> result = ApiResult.error(ResultCodeEnum.USER_NO_EXPRESSION.getCode(), "权限不足，无法访问资源");
        String jsonResult = JSON.toJSONString(result);
        ServletUtil.renderString(response, jsonResult);
    }
}
